import { Component, ViewChild } from '@angular/core';
import {MenuController, Nav, Platform, ToastController} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';


import { TabsPage } from '../pages/tabs/tabs';
import {BackgroundGeolocation, BackgroundGeolocationResponse, BackgroundGeolocationConfig} from "@ionic-native/background-geolocation";
import { BackgroundMode } from '@ionic-native/background-mode';
import {LoginService} from "../pages/login/login.service";

@Component({
  providers:[LoginService],
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  rootPage:any = TabsPage;
  backButtonPressed: boolean = false;
  private config: BackgroundGeolocationConfig = {
  desiredAccuracy: 10,
  stationaryRadius: 20,
  distanceFilter: 30,
  debug: true, //  enable this hear sounds for background-geolocation life-cycle.
  stopOnTerminate: false, // enable this to clear background location settings when the app terminates
};

  constructor(private platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
              private backgroundGeolocation: BackgroundGeolocation,private backgroundMode: BackgroundMode,
              private menuCtrl: MenuController,private toastCtrl: ToastController,
  loginService: LoginService) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

      backgroundMode.enable();
      backgroundMode.overrideBackButton()
      backgroundMode.on("activate").subscribe(resp=>{
        setInterval(()=>{
          backgroundGeolocation.start();
          // loginService.login("http://10.10.122.45:8090/users/login","admin","123456");
          backgroundGeolocation.stop();
          },15000);

      },err=>{});

      backgroundGeolocation.configure(this.config)
        .subscribe((location: BackgroundGeolocationResponse) => {

          console.log(location);
          // loginService.login("http://10.10.122.45:8090/users/login","admin","123456");
          // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
          // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
          // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
          backgroundGeolocation.finish(); // FOR IOS ONLY

        });
      this.registerBackBtn();
    });
  }
  registerBackBtn(){
    this.platform.registerBackButtonAction((): any => {
      let activeVC = this.nav.getActive();
      let page = activeVC.instance;
      alert(this.menuCtrl.isOpen());
      if (this.menuCtrl.isOpen()) return this.menuCtrl.close();
alert(0);
      if (!(page instanceof TabsPage)) {
        alert(1);
        if (!this.nav.canGoBack()) {
          alert(2+"=="+!this.nav.canGoBack());
          //当前页面为tabs，退出APP
          return this.showExit();
        }
        alert(3);
        //当前页面为tabs的子页面，正常返回
        return this.nav.pop();
      }
      let tabs = page.tabs;
      let activeNav = tabs.getSelected();
      if (!activeNav.canGoBack()) {
        alert(4);
        //当前页面为tab栏，退出APP
        return this.showExit();
      }
      alert(5);
      //当前页面为tab栏的子页面，正常返回
      return activeNav.pop();
    }, 101);
  }
  showExit() {
    if (this.backButtonPressed) this.backgroundMode.moveToBackground();
    else {
      let toast = this.toastCtrl.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'bottom'
      });
      toast.present();
      this.backButtonPressed = true;
      setTimeout(() => {
        this.backButtonPressed = false;
      }, 2000)
    }
  }
}
